#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Archives a set of files.
"""

import os
import sys
import argparse
import subprocess


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--targetDir', required=True)
    parser.add_argument('--outputJar', required=True)
    parser.add_argument('--toolchain', required=True)
    parser.add_argument('--compileTarget', required=True)
    args = parser.parse_args()
    root_dir = os.path.dirname(os.path.realpath(__file__))
    toolchain = args.toolchain
    tool_list = toolchain.split(':')
    toolchain = tool_list[-1]
    toolchain += "_" + args.compileTarget
    compile_path = os.path.join(root_dir, "src/main/java/compile.sh")
    targetDir = os.path.join(root_dir, args.targetDir)
    time_out = 5000
    command_compile = ['bash', compile_path, root_dir, args.targetDir, args.outputJar, toolchain]
    child_compile = subprocess.Popen(command_compile, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    compile_out, compile_err = child_compile.communicate(timeout=time_out)
    if child_compile.returncode != 0:
        print(compile_out.decode('utf-8'))
        print(compile_err.decode('utf-8'))
        raise Exception("compile fastjson java class failed!")
    

if __name__ == '__main__':
    sys.exit(main())

